// ActionScript file

import common.events.GenericEvent;
import common.utils.StringManager;
import configuration.dataObjects.VendorDO;			
import general.components.VendorQF;

import inbound.receiving.components.CheckIn;
import inbound.receiving.components.RcpDtlQFCnt;
import inbound.receiving.dataObjects.ReceiptDetailDO;
import inbound.receiving.dataObjects.ReceiptHeaderDO;

import mx.collections.ArrayCollection;
import mx.containers.TitleWindow;
import mx.controls.Alert;
import mx.controls.Menu;
import mx.events.ListEvent;
import mx.events.MenuEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Bindable]private var _selectedReceiptHdr:ReceiptHeaderDO = new ReceiptHeaderDO();
[Bindable]private var _acRcpDtl:ArrayCollection = new ArrayCollection();	
private var _acRcpType:ArrayCollection = new ArrayCollection();
private var _acState:ArrayCollection = new ArrayCollection();
private var _selectedItem:ReceiptDetailDO = null;
private var _acVal:Array = new Array(); 
//private var _acPrint:Object = [{label:"打印验收单", data:"CheckIn"}, {label:"打印上架单", data:"Putaway"}];
private var _acDoc:ArrayCollection = new ArrayCollection();

[Bindable]
[Embed(source="assets/images/search_16x16.png")]
private var search_16x16:Class;

private function initData():void{
	var wsdl:String = this.parentApplication.AppServer + "GeneralinforService.asmx?wsdl";
	GeneralInfoSvs.loadWSDL(wsdl);
	wsdl = this.parentApplication.AppServer + "ReceivingService.asmx?wsdl";
	ReceivingSvs.loadWSDL(wsdl);
	
	GeneralInfoSvs.GetCompanies(this.parentApplication.SessionID, "");
	GeneralInfoSvs.GetStates(this.parentApplication.SessionID);
	GeneralInfoSvs.GetReceiptTypes(this.parentApplication.SessionID);
	GeneralInfoSvs.GetGenericConfigs(this.parentApplication.SessionID, "RECEIPT_DOCUMENT");
	
	_acVal.push(VAL_RECEIPT_ID);
	this.SelectedReceiptHdr = this._selectedReceiptHdr;
}

public function set SelectedReceiptHdr(value:ReceiptHeaderDO):void{
	this._selectedReceiptHdr = value;
	this._acRcpDtl.removeAll();
	this.dgList.dataProvider = this._acRcpDtl;
	if (this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM > 0){
		ReceivingSvs.GetReceiptDtls(this.parentApplication.SessionID, this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM);
	}
	else{
		this._selectedReceiptHdr.WAREHOUSE = this.parentApplication.CurrentWarehouse;
	}
	
	setDisplayProperty();
}	

//绑定省份下拉列表
private function bindState(event:ResultEvent):void{
	if (event.result != null){
		var stErr:String = event.result.toString();
		if (StringManager.IsErrorMsg(stErr)){
			Alert.show(stErr, "错误");
			return;
		}
		var xmlObj:XML = new XML(event.result.toString());
		
		for each(var xmlNode:XML in xmlObj.children()){
			_acState.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
		}
		this.SHIP_FROM_STATE.dataProvider = _acState;	
	}
}

//绑定收货单类型下拉列表
private function bindReceiptType(event:ResultEvent):void{
	var xmlObj:XML = new XML(event.result.toString());
	
	for each(var xmlNode:XML in xmlObj.children()){
		_acRcpType.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
	}
	this.RECEIPT_TYPE.dataProvider = _acRcpType;	
}

//绑定打印列表
private function bindReceiptDocument(event:ResultEvent):void{
	var xmlObj:XML = new XML(event.result.toString());
	
	for each(var xmlNode:XML in xmlObj.children()){
		_acDoc.addItem({label:xmlNode.DESCRIPTION.toString(), data:xmlNode.IDENTIFIER.toString()});
	}
	
	var popMenu:Menu = new Menu();
	popMenu.dataProvider = _acDoc;
	popMenu.addEventListener("itemClick", menuClickHandler);
	this.btnPrint.popUp = popMenu;
	//this.btnPrint.dataProvider = _acDoc;
}

private function menuClickHandler(event:MenuEvent):void{
	var docType:String = event.item.data;
	var reportApp:String = this.parentApplication.ReportServer + "DynamicReportViewer.aspx?rptType=" + docType + "&paras=INTERNAL_RECEIPT_NUM," 
		+ this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM + "&sessID=" + this.parentApplication.SessionID;
	var url:URLRequest = new URLRequest(reportApp);
	navigateToURL(url);
}

//绑定收货单明细数据表格
private function bindReceiptDtl(event:ResultEvent):void{
	if (null != event.result){				
		var errMsg:String = event.result as String;
		
		if (StringManager.IsErrorMsg(errMsg))
			Alert.show(errMsg, "错误");
		else{
			var xmlObj:XML = new XML(errMsg);
			_acRcpDtl = ReceiptDetailDO.getReceiptDetails(xmlObj.RECEIPT_DETAIL);
			dgList.dataProvider = _acRcpDtl;
		}
	}
}

private function onFault(event:FaultEvent):void{
	Alert.show(event.message.toString(), "错误");
}

private function setDisplayProperty():void{
	if (this._selectedReceiptHdr != null && 
		this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM > 0){
		this.RECEIPT_ID.editable = false;
		this.RECEIPT_TYPE.enabled = false;
		this.COMPANY.enabled = false;
	}
	else{
		this.RECEIPT_ID.editable = true;
		this.RECEIPT_TYPE.enabled = true;
		this.COMPANY.enabled = true;
	}
}

private function validateForm():Boolean {
	var arError:Array = Validator.validateAll(this._acVal);;
	var isValid:Boolean = (arError.length == 0);
	if (isValid)
		return true;
	else {
		var event:ValidationResultEvent;
		var arErrMsg:Array = [];
		for each (event in arError) {
			//var errField:String = FormItem(event.currentTarget.source.parent).label;
			arErrMsg.push(event.message);
		}
		Alert.show(arErrMsg.join("\n"), "错误", Alert.OK);
		return false;
	}
} 

//保存
private function btnSave_Click():void{
	if (validateForm()){
		var stXML:String = this.checkModel.toString();
		ReceivingSvs.SaveReceiptHdr(this.parentApplication.SessionID, stXML);
	}
}

private function onSaveResult(event:ResultEvent):void{
	if (null != event.result){				
		var errMsg:String = event.result as String;
		
		if (StringManager.IsErrorMsg(errMsg))
			Alert.show(errMsg, "错误");
		else{
			this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM = Number(errMsg);
			this.INTERNAL_RECEIPT_NUM.text = errMsg;
			Alert.show("保存成功", "信息");
		}
	}
}

private function btnAddDetail_Click():void{
	if (this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM == 0){
		Alert.show("请先保存主单据", "错误");
		return;
	}
	var rcpDtlObj:ReceiptDetailDO = new ReceiptDetailDO();
	rcpDtlObj.INTERNAL_RECEIPT_NUM = this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM;
	rcpDtlObj.WAREHOUSE = this.WAREHOUSE.text;//this._selectedReceiptHdr.WAREHOUSE;
	if (this.COMPANY.selectedItem.data != null)
		rcpDtlObj.COMPANY = this.COMPANY.selectedItem.data;//this._selectedReceiptHdr.COMPANY;
	rcpDtlObj.RECEIPT_ID = this.RECEIPT_ID.text;//this._selectedReceiptHdr.RECEIPT_ID;
	rcpDtlObj.ERP_ORDER_ID = this.ERP_ORDER_ID.text;//this._selectedReceiptHdr.ERP_ORDER_ID;
	
	var detailDlg:RcpDtlQFCnt =    
		RcpDtlQFCnt(PopUpManager.createPopUp(this, RcpDtlQFCnt, true));   
	detailDlg.mainApp = this;
	detailDlg.title = "收货单明细";
	detailDlg.SelectedReceiptDetail = rcpDtlObj;
	detailDlg.callbackFunction = this.refresh;   
	
	PopUpManager.centerPopUp(detailDlg); 
	detailDlg.x = ( this.parentApplication.width - detailDlg.width ) / 2;
	detailDlg.y = ( this.parentApplication.height - detailDlg.height ) / 2;
}



private function refresh():void{
	ReceivingSvs.GetReceiptDtls(this.parentApplication.SessionID, this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM);				
}

private function btnCheckIn_Click():void{
	if (this._acRcpDtl == null ||
		this._acRcpDtl.length < 1){
		Alert.show("收货单无明细", "错误");
		return;
	}
	/*
	var event:GenericEvent = new GenericEvent("checkInClick", this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM);
	this.dispatchEvent(event);
	*/
	var checkInDlg:CheckIn = 
		CheckIn(PopUpManager.createPopUp(this.parent, CheckIn, true));   
	
	checkInDlg.InternalReceiptNum = this._selectedReceiptHdr.INTERNAL_RECEIPT_NUM;
	PopUpManager.centerPopUp(checkInDlg);
	checkInDlg.x = ( this.parentApplication.width - checkInDlg.width ) / 2;
	checkInDlg.y = ( this.parentApplication.height - checkInDlg.height ) / 2;
}

private function btnDelete_Click():void{
	if (this.dgList.selectedItem == null){
		Alert.show("请选择要删除收货单行", "错误");
		return;
	}
	_selectedItem = this.dgList.selectedItem as ReceiptDetailDO;
	ReceivingSvs.DeleteReceiptDetail(this.parentApplication.SessionID, _selectedItem.INTERNAL_RECEIPT_LINE_NUM);
}

private function onDeleteResult(event:ResultEvent):void{
	if (event.result != null && event.result.toString() != ""){
		Alert.show(event.result.toString(), "错误");					
	}
	else{
		this._acRcpDtl.removeItemAt(this._acRcpDtl.getItemIndex(_selectedItem));
		this.dgList.dataProvider = this._acRcpDtl;
	}
}

//清空界面内容以及明细列表
private function btnClear_Click():void{
	this._selectedReceiptHdr = null;
	this._acRcpDtl = null;
	this.dgList.dataProvider = null;
}

private function itemDoubleClickHandler(event:ListEvent):void{
	var rcpDtlDO:ReceiptDetailDO = event.itemRenderer.data as ReceiptDetailDO;
	
	var detailDlg:RcpDtlQFCnt =    
		RcpDtlQFCnt(PopUpManager.createPopUp(this.parent, RcpDtlQFCnt, true));  
	
	detailDlg.mainApp = this;
	detailDlg.title = "收货单明细";
	detailDlg.SelectedReceiptDetail = rcpDtlDO;
	//titleWindowInstance.callbackFunction = this.myFunction;   
	
	PopUpManager.centerPopUp(detailDlg);
	detailDlg.x = ( this.parentApplication.width - detailDlg.width ) / 2;
	detailDlg.y = ( this.parentApplication.height - detailDlg.height ) / 2;
	
}

private function btnPrint_Click():void{	
	
	btnPrint.close();
} 

private function btnLookupVendor_Click():void{
	var lookupDlg:VendorQF = VendorQF(PopUpManager.createPopUp(this, VendorQF, false));
	
	lookupDlg.vendor_id = this.SHIP_FROM.text;
	lookupDlg.vendor_name = this.SHIP_FROM_NAME.text;
	lookupDlg.VENDOR_ID.text = this.SHIP_FROM.text;
	lookupDlg.NAME.text = this.SHIP_FROM_NAME.text;
	lookupDlg.mainApp = this;
	lookupDlg.title = "供应商查找"; 
	lookupDlg.callbackFunction = this.bindVendor;   
	
	PopUpManager.centerPopUp(lookupDlg);
	lookupDlg.x = ( this.parentApplication.width - lookupDlg.width ) / 2;
	lookupDlg.y = ( this.parentApplication.height - lookupDlg.height ) / 2;
}

private function bindVendor(vendor:VendorDO):void{
	this.SHIP_FROM.text = vendor.VENDOR_ID;
	this.SHIP_FROM_ADDRESS1.text = vendor.ADDRESS1;
	this.SHIP_FROM_ADDRESS2.text = vendor.ADDRESS2;
	this.SHIP_FROM_ATTENTION_TO.text = vendor.ATTENTION_TO;
	this.SHIP_FROM_CITY.text = vendor.CITY;
	this.SHIP_FROM_COUNTRY.text = vendor.COUNTRY;
	this.SHIP_FROM_EMAIL_ADDRESS.text = vendor.EMAIL;
	this.SHIP_FROM_FAX_NUM.text = vendor.FAX_NUM;
	this.SHIP_FROM_NAME.text = vendor.NAME;
	this.SHIP_FROM_PHONE_NUM.text = vendor.PHONE_NUM;
	this.SHIP_FROM_POSTAL_CODE.text = vendor.POSTAL_CODE;
	this.SHIP_FROM_STATE.selectedData = vendor.STATE;
}